﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "InplaceEditSharedContent.h"

#include "DbObjectUtils.h"
#include "NdbUpgrade.h"
#include "NdbCustomUpgrader.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

BEGIN_UPGRADE_CLASS(gcmp, InplaceEditSharedViewContent, 2)
ADD_ARR_PROPERTY(OwnerPtr<InplaceEditSharedFillPatternInfo>, FillPatternInfo)
ADD_ARR_PROPERTY(OwnerPtr<InplaceEditSharedLineTypeNewInfo>, LineTypeNewInfo)
END_UPGRADE_CLASS

BEGIN_UPGRADE_CLASS(gcmp, InplaceEditSharedGraphicsStyleInfo, 2)
ADD_NON_OBJECT_PROPERTY(double, ProjectionFaceHatchLineWidth)
ADD_NON_OBJECT_PROPERTY(Bool, IsProjectionLineColorAlphaEnabled)
END_UPGRADE_CLASS

BEGIN_UPGRADE_CLASS(gcmp, InplaceEditSharedGraphicsStyleInfo, 3)
ADD_NON_OBJECT_PROPERTY(Bool, IsProjectionLineWidthModePixel)
ADD_NON_OBJECT_PROPERTY(double, ProjectionLineTypeScale)
ADD_NON_OBJECT_PROPERTY(Bool, IsSectionLineWidthModePixel)
ADD_NON_OBJECT_PROPERTY(double, SectionLineTypeScale)
ADD_NON_OBJECT_PROPERTY(Color, HiddenLineColor)
ADD_NON_OBJECT_PROPERTY(Bool, IsHiddenLineColorSmart)
ADD_NON_OBJECT_PROPERTY(double, HiddenLineWidth)
ADD_NON_OBJECT_PROPERTY(Bool, IsHiddenLineWidthModePixel)
ADD_NON_OBJECT_PROPERTY(std::wstring, HiddenLineTypeName)
ADD_NON_OBJECT_PROPERTY(double, HiddenLineTypeScale)
END_UPGRADE_CLASS

BEGIN_UPGRADE_CLASS(gcmp, InplaceEditSharedGraphicsStyleInfo, 4)
ADD_NON_OBJECT_PROPERTY(SmartColorMode, ProjectionLineColorSmart)
ADD_NON_OBJECT_PROPERTY(SmartColorMode, SectionLineColorSmart)
ADD_NON_OBJECT_PROPERTY(SmartColorMode, HiddenLineColorSmart)
BEGIN_UPGRADE_PROPERTY(customUpgraderOldObject, customUpgraderNewObject)
{
    bool result = false;
    Bool isColorSmart;
    result = customUpgraderOldObject.GetPropertyValue<Bool>(L"IsProjectionLineColorSmart", isColorSmart);
    DBG_WARN_AND_RETURN_UNLESS(result, false, L"customUpgraderOldObject版本6中没有属性IsProjectionLineColorSmart",L"GDMPLab",L"2024-03-30");
    if (isColorSmart)
    {
        result = customUpgraderNewObject.SetPropertyValue<SmartColorMode>(L"ProjectionLineColorSmart", SmartColorMode::SmartAntiBackgroundColor);
        DBG_WARN_AND_RETURN_UNLESS(result, false, L"设置CustomizedInternalObject版本2中属性ProjectionLineColorSmart的新值失败",L"GDMPLab",L"2024-03-30");
    }

    result = customUpgraderOldObject.GetPropertyValue<Bool>(L"IsSectionLineColorSmart", isColorSmart);
    DBG_WARN_AND_RETURN_UNLESS(result, false, L"customUpgraderOldObject版本6中没有属性IsSectionLineColorSmart",L"GDMPLab",L"2024-03-30");
    if (isColorSmart)
    {
        result = customUpgraderNewObject.SetPropertyValue<SmartColorMode>(L"SectionLineColorSmart", SmartColorMode::SmartAntiBackgroundColor);
        DBG_WARN_AND_RETURN_UNLESS(result, false, L"设置CustomizedInternalObject版本2中属性SectionLineColorSmart的新值失败",L"GDMPLab",L"2024-03-30");
    }

    result = customUpgraderOldObject.GetPropertyValue<Bool>(L"IsHiddenLineColorSmart", isColorSmart);
    DBG_WARN_AND_RETURN_UNLESS(result, false, L"customUpgraderOldObject版本6中没有属性IsHiddenLineColorSmart",L"GDMPLab",L"2024-03-30");
    if (isColorSmart)
    {
        result = customUpgraderNewObject.SetPropertyValue<SmartColorMode>(L"HiddenLineColorSmart", SmartColorMode::SmartAntiBackgroundColor);
        DBG_WARN_AND_RETURN_UNLESS(result, false, L"设置CustomizedInternalObject版本2中属性HiddenLineColorSmart的新值失败",L"GDMPLab",L"2024-03-30");
    }
    return result;
}
END_UPGRADE_PROPERTY
DELETE_PROPERTY(IsProjectionLineColorSmart)
DELETE_PROPERTY(IsSectionLineColorSmart)
DELETE_PROPERTY(IsHiddenLineColorSmart)
END_UPGRADE_CLASS


DBOBJECT_DATA_DEFINE(InplaceEditSharedModelViewInfo)
{

}

gcmp::InplaceEditSharedModelViewInfo::InplaceEditSharedModelViewInfo(const std::wstring& viewType, const std::wstring& viewName, BuiltInViewType builtInViewType)
{
    this->SetViewType(viewType);
    this->SetViewName(viewName);
    this->SetBuiltInViewType(builtInViewType);
}

DBOBJECT_DATA_DEFINE(InplaceEditSharedGraphicsStyleInfo)
{
    SetSectionFaceHatchRotation__(0);
    SetProjectionLineWidth__(0);
    SetSectionLineWidth__(0);
    SetProjectionFaceHatchScale__(0);
    SetProjectionFaceHatchRotation__(0);
    SetSectionFaceHatchScale__(0);
    SetSectionFaceHatchRotation__(0);
    SetProjectionFaceHatchLineWidth__(1.0);
    SetProjectionLineTypeScale__(1.0);
    SetSectionLineTypeScale__(1.0);
    SetIsProjectionLineWidthModePixel__(true);
    SetIsSectionLineWidthModePixel__(true);
    SetHiddenLineWidth__(0);
    SetHiddenLineTypeScale__(1.0);
    SetIsHiddenLineWidthModePixel__(true);
    SetHiddenLineColor__(Color(0, 0, 0));
    SetHiddenLineColorSmart__(SmartColorMode::Invalid);
    SetHiddenLineTypeName__(L"DASHED");
    SetIsProjectionLineColorAlphaEnabled__(false);
}

gcmp::InplaceEditSharedGraphicsStyleInfo::InplaceEditSharedGraphicsStyleInfo(const IGraphicsStyleData* pStyleData)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pStyleData, L"pStyleData参数为空",L"GDMPLab",L"2024-03-30");

    SetColor__(pStyleData->GetColor());
    SetTransparency__(pStyleData->GetTransparency());
    SetProjectionLineColor__(pStyleData->GetProjectionLineColor());
    SetIsProjectionLineColorAlphaEnabled__(pStyleData->IsProjectionLineColorAlphaEnabled());
    SetProjectionLineWidth__(pStyleData->GetProjectionLineWidth());
    SetProjectionLineTypeName__(pStyleData->GetProjectionLineTypeName());
    SetSectionLineColor__(pStyleData->GetSectionLineColor());
    SetSectionLineWidth__(pStyleData->GetSectionLineWidth());
    SetSectionLineTypeName__(pStyleData->GetSectionLineTypeName());
    SetHiddenLineColor__(pStyleData->GetHiddenLineColor());
    SetHiddenLineWidth__(pStyleData->GetHiddenLineWidth());
    SetHiddenLineTypeName__(pStyleData->GetHiddenLineTypeName());
    SetProjectionFaceHatchColor__(pStyleData->GetProjectionFaceHatchColor());
    SetProjectionFaceHatchPattern__(pStyleData->GetProjectionFaceHatchPattern());
    SetProjectionFaceHatchScale__(pStyleData->GetProjectionFaceHatchScale());
    SetProjectionFaceHatchRotation__(pStyleData->GetProjectionFaceHatchRotation());
    SetSectionFaceHatchColor__(pStyleData->GetSectionFaceHatchColor());
    SetSectionFaceHatchPattern__(pStyleData->GetSectionFaceHatchPattern());
    SetSectionFaceHatchScale__(pStyleData->GetSectionFaceHatchScale());
    SetSectionFaceHatchRotation__(pStyleData->GetSectionFaceHatchRotation());
    SetProjectionFaceHatchLineWidth__(pStyleData->GetProjectionFaceHatchLineWidth());
    SetProjectionLineTypeScale__(pStyleData->GetProjectionLineTypeScale());
    SetSectionLineTypeScale__(pStyleData->GetSectionLineTypeScale());
    SetHiddenLineTypeScale__(pStyleData->GetHiddenLineTypeScale());
    SetIsProjectionLineWidthModePixel__(pStyleData->GetProjectionLineWidthMode() == LineWidthMode::PIXEL);
    SetIsSectionLineWidthModePixel__(pStyleData->GetSectionLineWidthMode() == LineWidthMode::PIXEL);
    SetIsHiddenLineWidthModePixel__(pStyleData->GetHiddenLineWidthMode() == LineWidthMode::PIXEL);
    SetHiddenLineColorSmart__(pStyleData->GetHiddenLineSmartColorMode());
}

OwnerPtr<IGraphicsStyleData> gcmp::InplaceEditSharedGraphicsStyleInfo::GetAsGraphicsStyleData() const
{
    LineWidthMode projectionLineWidthMode = GetIsProjectionLineWidthModePixel__() ? LineWidthMode::PIXEL : LineWidthMode::WORLD;
    LineWidthMode sectionLineWidthMode = GetIsSectionLineWidthModePixel__() ? LineWidthMode::PIXEL : LineWidthMode::WORLD;
    LineWidthMode hideLineWidthMode = GetIsHiddenLineWidthModePixel__() ? LineWidthMode::PIXEL : LineWidthMode::WORLD;

    OwnerPtr<IGraphicsStyleData> opStyleData = IGraphicsStyleData::Create();
    opStyleData->Reset(GetColor__(), GetTransparency__(), GetProjectionLineColor__(), GetProjectionLineWidth__()
        , GetProjectionLineTypeName__(), GetSectionLineColor__(), GetSectionLineWidth__(), GetSectionLineTypeName__()
        , GetProjectionFaceHatchColor__(), GetProjectionFaceHatchPattern__(), GetProjectionFaceHatchScale__()
        , GetProjectionFaceHatchRotation__(), GetSectionFaceHatchColor__(), GetSectionFaceHatchPattern__()
        , GetSectionFaceHatchScale__(), GetSectionFaceHatchRotation__(), projectionLineWidthMode, sectionLineWidthMode
        , GetProjectionLineTypeScale__(), GetSectionLineTypeScale__(), GetProjectionFaceHatchLineWidth__());
    opStyleData->EnableProjectionLineColorAlpha(GetIsProjectionLineColorAlphaEnabled__());
    opStyleData->SetHiddenLineColor(GetHiddenLineColor__());
    opStyleData->SetHiddenLineWidth(GetHiddenLineWidth__());
    opStyleData->SetHiddenLineTypeName(GetHiddenLineTypeName__());
    opStyleData->SetHiddenLineTypeScale(GetHiddenLineTypeScale__());
    opStyleData->SetHiddenLineSmartColorMode(GetHiddenLineColorSmart__());
    opStyleData->SetHiddenLineWidthMode(hideLineWidthMode);
    return opStyleData;
}


DBOBJECT_DATA_DEFINE(InplaceEditSharedGraphicsMaterialInfo)
{
    SetTransparency__(0);
    SetShininess__(0);
    SetDiffuseTexureRotation__(0);
}

gcmp::InplaceEditSharedGraphicsMaterialInfo::InplaceEditSharedGraphicsMaterialInfo(const IGraphicsMaterialData* pMaterialData)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pMaterialData, L"pMaterialData参数为空",L"GDMPLab",L"2024-03-30");

    SetAmbient__(pMaterialData->GetAmbient());
    SetDiffuse__(pMaterialData->GetDiffuse());
    SetSpecular__(pMaterialData->GetSpecular());
    SetTransparency__(pMaterialData->GetTransparency());
    SetShininess__(pMaterialData->GetShininess());
    SetIsDiffuseTextureEnabled__(pMaterialData->IsDiffuseTextureEnabled());
    SetDiffuseTextureFilePath__(pMaterialData->GetDiffuseTextueFilePath());
    SetDiffuseTexureOffset__(pMaterialData->GetDiffuseTexureOffset());
    SetDiffuseTexureRotation__(pMaterialData->GetDiffuseTexureRotation());
    SetDiffuseTexureScale__(pMaterialData->GetDiffuseTexureScale());
}

OwnerPtr<IGraphicsMaterialData> gcmp::InplaceEditSharedGraphicsMaterialInfo::GetAsGraphicsMaterialData() const
{
    OwnerPtr<IGraphicsMaterialData> opMaterialData = IGraphicsMaterialData::Create();
    opMaterialData->Reset(GetAmbient__(), GetDiffuse__(), GetSpecular__(), GetTransparency__(), GetShininess__()
        , GetIsDiffuseTextureEnabled__(), GetDiffuseTextureFilePath__(), GetDiffuseTexureOffset__()
        , GetDiffuseTexureRotation__(), GetDiffuseTexureScale__());
    return opMaterialData;
}

DBOBJECT_DATA_DEFINE(InplaceEditSharedFillPatternInfo)
{
}

gcmp::InplaceEditSharedFillPatternInfo::InplaceEditSharedFillPatternInfo(const IFillPatternData* pFillPatternData)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pFillPatternData, L"pFillPatternData参数为空",L"GDMPLab",L"2024-03-30");
    SetName__(pFillPatternData->GetName());
    SetDescription__(pFillPatternData->GetDescription());
    SetPatternString__(pFillPatternData->GetPatternString());
    SetScale__(pFillPatternData->GetScale());
    SetRotateAngle__(pFillPatternData->GetRotateAngle());
    SetIsSolid__(pFillPatternData->IsSolid());
    SetTarget__(pFillPatternData->GetTarget());
}

OwnerPtr<IFillPatternData> gcmp::InplaceEditSharedFillPatternInfo::GetAsFillPatternData() const
{
    OwnerPtr<IFillPatternData> opFillPatternData = IFillPatternData::CreateByName(GetName__());
    opFillPatternData->SetDescription(GetDescription__());
    opFillPatternData->SetIsSolid(GetIsSolid__());
    opFillPatternData->SetPatternString(GetPatternString__());
    opFillPatternData->SetRotateAngle(GetRotateAngle__());
    opFillPatternData->SetScale(GetScale__());
    opFillPatternData->SetTarget(GetTarget__());
    return opFillPatternData;
}
DBOBJECT_DATA_DEFINE(InplaceEditSharedLineTypeNewInfo)
{
}

gcmp::InplaceEditSharedLineTypeNewInfo::InplaceEditSharedLineTypeNewInfo(const ILineTypeData* pLineTypeData)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pLineTypeData, L"pLineTypeData参数为空",L"GDMPLab",L"2024-03-30");
    SetName__(pLineTypeData->GetName());
    SetDescription__(pLineTypeData->GetDescription());
    SetPatternStr__(pLineTypeData->GetPatternString());
}

OwnerPtr<ILineTypeData> gcmp::InplaceEditSharedLineTypeNewInfo::GetAsLineTypeData() const
{
    OwnerPtr<ILineTypeData> opLineTypeData = ILineTypeData::CreateByName(GetName__(), GetDescription__(), GetPatternStr__());
    return opLineTypeData;
}

DBOBJECT_DATA_DEFINE(InplaceEditSharedCategoryInfo)
{

}

DBOBJECT_DATA_DEFINE(InplaceEditSharedContent)
{
    SetProjectAppPort__(0);
}

DBOBJECT_DATA_DEFINE(InplaceEditSharedElementInfo)
{

}

DBOBJECT_DATA_DEFINE(InplaceEditSharedCameraInfo)
{

}

DBOBJECT_DATA_DEFINE(InplaceEditSharedViewContent)
{

}

DBOBJECT_DATA_DEFINE(InplaceEditSharedJournalContent)
{

}

bool gcmp::InplaceEditSharedViewContent::IsStyleExist(const ElementId & styleId) const
{
    FOR_EACH(opStyleData, GetStyleInfo__())
    {
        if (opStyleData->GetStyleId() == styleId)
        {
            return true;
        }
    }
    return false;
}

bool gcmp::InplaceEditSharedViewContent::IsFillPatternExist(const ElementId & genericId) const
{
    FOR_EACH(opFillPatternData, GetFillPatternInfo__())
    {
        if (opFillPatternData->GetGenericId() == genericId)
        {
            return true;
        }
    }
    return false;
}

bool gcmp::InplaceEditSharedViewContent::IsLineTypeNewExist(const ElementId & genericId) const
{
    FOR_EACH(opLineTypenewData, GetLineTypeNewInfo__())
    {
        if (opLineTypenewData->GetGenericId() == genericId)
        {
            return true;
        }
    }
    return false;
}

bool gcmp::InplaceEditSharedViewContent::IsMaterialExist(const ElementId & materialId) const
{
    FOR_EACH(opMaterialData, GetMaterialInfo__())
    {
        if (opMaterialData->GetMaterialId() == materialId)
        {
            return true;
        }
    }
    return false;
}

bool gcmp::InplaceEditSharedViewContent::AddStyleInfo(const ElementId & styleId, const IGraphicsStyleData * pStyleData)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(styleId.IsValid(), L"styleId参数非法",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStyleData, L"pStyleData参数为空",L"GDMPLab",L"2024-03-30");
    if (IsStyleExist(styleId))
    {
        return false;
    }
    OwnerPtr<InplaceEditSharedGraphicsStyleInfo> opStyleInfo = NEW_AS_OWNER_PTR(InplaceEditSharedGraphicsStyleInfo, pStyleData);
    opStyleInfo->SetStyleId(styleId);
    GetStyleInfoFW__().push_back(TransferOwnership(opStyleInfo));
    return true;
}

bool gcmp::InplaceEditSharedViewContent::AddMaterialInfo(const ElementId & materialId, const IGraphicsMaterialData * pMaterialData)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(materialId.IsValid(), L"materialId参数非法",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pMaterialData, L"pMaterialData参数为空",L"GDMPLab",L"2024-03-30");
    if (IsMaterialExist(materialId))
    {
        return false;
    }
    OwnerPtr<InplaceEditSharedGraphicsMaterialInfo> opMaterialInfo = NEW_AS_OWNER_PTR(InplaceEditSharedGraphicsMaterialInfo, pMaterialData);
    opMaterialInfo->SetMaterialId(materialId);
    GetMaterialInfoFW__().push_back(TransferOwnership(opMaterialInfo));
    return true;
}

bool gcmp::InplaceEditSharedViewContent::AddFillPatternInfo(const ElementId & genericId, const IFillPatternData * pFillPatternData)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(genericId.IsValid(), L"genericId参数非法",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFillPatternData, L"pFillPatternData参数为空",L"GDMPLab",L"2024-03-30");
    if (IsFillPatternExist(genericId))
    {
        return false;
    }
    OwnerPtr<InplaceEditSharedFillPatternInfo> opFillPatternInfo = NEW_AS_OWNER_PTR(InplaceEditSharedFillPatternInfo, pFillPatternData);
    opFillPatternInfo->SetGenericId(genericId);
    GetFillPatternInfoFW__().push_back(TransferOwnership(opFillPatternInfo));
    return true;
}

bool gcmp::InplaceEditSharedViewContent::AddLineTypeNewInfo(const ElementId & genericId, const ILineTypeData * pLineTypeData)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(genericId.IsValid(), L"genericId参数非法",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pLineTypeData, L"pLineTypeData参数为空",L"GDMPLab",L"2024-03-30");
    if (IsLineTypeNewExist(genericId))
    {
        return false;
    }
    OwnerPtr<InplaceEditSharedLineTypeNewInfo> opLineTypeNewInfo = NEW_AS_OWNER_PTR(InplaceEditSharedLineTypeNewInfo, pLineTypeData);
    opLineTypeNewInfo->SetGenericId(genericId);
    GetLineTypeNewInfoFW__().push_back(TransferOwnership(opLineTypeNewInfo));
    return true;
}

const std::vector<OwnerPtr<InplaceEditSharedGraphicsStyleInfo>>& gcmp::InplaceEditSharedViewContent::GetStyleInfos() const
{
    return GetStyleInfo__();
}

const std::vector<OwnerPtr<InplaceEditSharedGraphicsMaterialInfo>>& gcmp::InplaceEditSharedViewContent::GetMaterialInfos() const
{
    return GetMaterialInfo__();
}

const std::vector<OwnerPtr<InplaceEditSharedFillPatternInfo>>& gcmp::InplaceEditSharedViewContent::GetFillPatternInfos() const
{
    return GetFillPatternInfo__();
}

const std::vector<OwnerPtr<InplaceEditSharedLineTypeNewInfo>>& gcmp::InplaceEditSharedViewContent::GetLineTypeNewInfos() const
{
    return GetLineTypeNewInfo__();
}